package com.yogee.topseller.activity.my;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.AccountManagerAdapter;
import com.yogee.topseller.bean.DeleteCardBean;
import com.yogee.topseller.bean.GetBankInfoBean;
import com.yogee.topseller.bean.SetIsFirstBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AccountManagerActivity extends HttpActivity {
    @BindView(R.id.manager_recycler)
    RecyclerView managerRecycler;
    @BindView(R.id.manager_titlelayout)
    TitleLayout titlelayout;
    private List<GetBankInfoBean.BankListBean> dataBeen = new ArrayList<>();
    private AccountManagerAdapter accountManagerAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_account_manager;
    }

    @Override
    protected void initView() {
        titlelayout.setTitle("收款账户管理");
        titlelayout.setActivity(this);
        accountManagerAdapter = new AccountManagerAdapter(this);
        managerRecycler.setLayoutManager(new LinearLayoutManager(this));
        managerRecycler.setAdapter(accountManagerAdapter);

        //设为默认
        accountManagerAdapter.setClickDefaultListener(new AccountManagerAdapter.clickDefaultListener() {
            @Override
            public void isDefault(String id, int position) {
                setIsFirst(AppUtil.getUserId(AccountManagerActivity.this), id);
            }
        });
        //删除
        accountManagerAdapter.setClickDeletetListener(new AccountManagerAdapter.clickDeletetListener() {
            @Override
            public void isDelete(String id, int position) {
                deleteCard(AppUtil.getUserId(AccountManagerActivity.this), id, position);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBankInfo(AppUtil.getUserId(this));
    }

    private void getBankInfo(String userId) {
        HttpManager.getInstance().getBankInfo(userId)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<GetBankInfoBean>() {
            @Override
            public void onNext(GetBankInfoBean o) {
                AccountManagerActivity.this.loadingFinished();
                dataBeen = o.getBankList();
                accountManagerAdapter.setBankListBeen(dataBeen);
            }
        }, this));
    }

    private void deleteCard(String userId, String id, final int position) {
        HttpManager.getInstance().deleteCard(userId, id)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<DeleteCardBean>() {
            @Override
            public void onNext(DeleteCardBean o) {
                AccountManagerActivity.this.loadingFinished();
                dataBeen.remove(position);
                accountManagerAdapter.notifyDataSetChanged();
            }
        }, this));
    }

    private void setIsFirst(String userId, String id) {
        HttpManager.getInstance().setIsFirst(userId, id)
                .compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<SetIsFirstBean>() {
            @Override
            public void onNext(SetIsFirstBean o) {
                AccountManagerActivity.this.loadingFinished();
                if ("1".equals(o.getResult())) {
                    getBankInfo(AppUtil.getUserId(AccountManagerActivity.this));
                }
            }
        }, this));
    }
    
}